/**
 *
 */
package org.suren.littlebird.views.sorter;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.suren.littlebird.po.SuRenBundle;

/**
 * @author fy911
 *
 */
public class DefaultViewerSorter extends ViewerSorter {

	private static final int ID = 1;
	private static final int NAME = 2;
	private static final int VERSION = 3;
	private static final int STATUS = 4;

	public static final DefaultViewerSorter ID_ASC = new DefaultViewerSorter(ID);
	public static final DefaultViewerSorter ID_DESC = new DefaultViewerSorter(
			-ID);
	public static final DefaultViewerSorter NAME_ASC = new DefaultViewerSorter(
			NAME);
	public static final DefaultViewerSorter NAME_DESC = new DefaultViewerSorter(
			-NAME);
	public static final DefaultViewerSorter VERSION_ASC = new DefaultViewerSorter(
			VERSION);
	public static final DefaultViewerSorter VERSION_DESC = new DefaultViewerSorter(
			-VERSION);
	public static final DefaultViewerSorter STATUS_ASC = new DefaultViewerSorter(
			STATUS);
	public static final DefaultViewerSorter STATUS_DESC = new DefaultViewerSorter(
			-STATUS);
	private int sortType;

	private DefaultViewerSorter(int sortType) {
		this.sortType = sortType;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.
	 * viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(Viewer viewer, Object e1, Object e2) {
		SuRenBundle bundle1 = (SuRenBundle) e1;
		SuRenBundle bundle2 = (SuRenBundle) e2;
		switch (sortType) {
		case ID: {
			Long l1 = bundle1.getId();
			Long l2 = bundle2.getId();
			return l1.compareTo(l2);
		}
		case -ID: {
			Long l1 = bundle1.getId();
			Long l2 = bundle2.getId();
			return l2.compareTo(l1);
		}
		case NAME: {
			String s1 = bundle1.getName();
			String s2 = bundle2.getName();
			return s1.compareTo(s2);
		}
		case -NAME: {
			String s1 = bundle1.getName();
			String s2 = bundle2.getName();
			return s2.compareTo(s1);
		}
		case VERSION: {
			String s1 = bundle1.getVersion();
			String s2 = bundle2.getVersion();
			return s1.compareTo(s2);
		}
		case -VERSION: {
			String s1 = bundle1.getVersion();
			String s2 = bundle2.getVersion();
			return s2.compareTo(s1);
		}
		case STATUS: {
			Integer i1 = bundle1.getState();
			Integer i2 = bundle2.getState();
			return i1.compareTo(i2);
		}
		case -STATUS: {
			Integer i1 = bundle1.getState();
			Integer i2 = bundle2.getState();
			return i2.compareTo(i1);
		}

		}
		return 0;
		// return super.compare(viewer, e1, e2);
	}

}
